//	ID : pallab81
// 	PROG : 10158
// 	LANG : C++
// 	2011-07-10-20.11.45 Sunday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

const int inf = (int)1e4;
int root[ inf*2 + 100 ];
int n;
int c,x,y;

inline void _set() {
    fo(i,inf*2+100) {
        root[i]=i;
    }
}
inline int find_root(int x) {
    if( x!= root[x] ) {
        root[x] = find_root( root[x] );
    }
    return root[x];
}

inline bool bad_friend(int x, int y) {
    int fx = find_root(x);
    int fy = find_root(y);
    int ex = find_root(x+inf);
    int ey = find_root(y+inf);

    // when x y is friend
    // CANNOT BE.....
    // friend of x is enemy of y
    // friend of y is enemy of x

    return ( fx==ey || fy==ex ) ;
}
inline bool bad_enemy(int x, int y) {
    int fx = find_root(x);
    int fy = find_root(y);
    int ex = find_root(x+inf);
    int ey = find_root(y+inf);
    // when x y is enemy
    // CANNOT BE
    // x y is friend
    // enemy of x and ememy of y is friend
    return ( fx==fy || ex == ey );
}
inline int set_friend(int x, int y) {
    if(bad_friend(x,y))return -1;

    int fx = find_root(x);
    int fy = find_root(y);
    int ex = find_root(x+inf);
    int ey = find_root(y+inf);

    //when friends
    root[ fy ] = fx; // friend of x is also friend of y
    root[ ey ] = ex; // enemy of x is also enemy of y
    return 1;
}

inline int set_enemy(int x, int y) {
    if(bad_enemy(x,y))return -1;

    int fx = find_root(x);
    int fy = find_root(y);
    int ex = find_root(x+inf);
    int ey = find_root(y+inf);


    // when enemy
    root[ ex ] = fy; // enemy of x is friend of y
    root[ ey ] = fx; // enemy of y is friend of x

    return 1;
}
inline int is_friend(int x, int y) {
    return ( find_root(x)==find_root(y)  );
}
inline int is_enemy(int x, int y) {
    int fx = find_root(x);
    int fy = find_root(y);
    int ex = find_root(x+inf);
    int ey = find_root(y+inf);

    // friend of x is friends with enemy of y
    // friend of y is friends with enemy of x
    return ( fx==ey || fy==ex ) ;
}
int main() {
    _set();
    for( scanf("%d",&n) ; scanf("%d %d %d",&c,&x,&y)==3, (c!=0 || x!=0 || y!=0) ;  ) {
        if( c==1 ) {
            if( set_friend(x,y)==-1 ) {
                puts("-1");
            }
        } else if( c==2 ) {
            if( set_enemy(x,y)==-1 ) {
                puts("-1");
            }
        } else if( c==3 ) {
            puts( is_friend(x,y)==1 ? "1":"0" );
        } else if( c==4 ) {
            puts( is_enemy(x,y)==1 ? "1" : "0" );
        }
    }

    return 0;
}

